package com.tensquare.manager.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class ManagerFilter extends ZuulFilter {


    //pre   在请求路由之前   post在之后
    @Override
    public String filterType() {
        return "pre";
    }

    //数字越少越先执行
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     *当前过滤器是否开启
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }


    /**
     *过滤器内执行的操作 return 任何object都表示继续执行
     *setsendzuulresp
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        System.out.println("aa");
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String header = request.getHeader("Authorization");
        if(StringUtils.isNotBlank(header)){
            //把头信息向下转
            context.addZuulRequestHeader("Authorization",header);
            //终止运行
//            context.setSendZuulResponse(false);
        }
        return null;
    }
}
